const ProductModel = require('../../models/productModel');

const ProductService = {
    //发布资源
    add: async ({ title, detail, cover, introduction, editTime, author }) => {
        return ProductModel.create({
            title,
            detail,
            cover,
            introduction,
            editTime,
            author,
        });
    },
    //获取单个用户的列表
    getList: async ({ author }) => {
        return ProductModel.find({ author });
    },
    // 删除文章
    delList: async ({ _id }) => {
        return ProductModel.deleteOne({ _id });
    },
    //根据资源id获取资源
    getListByID: async ({ _id }) => {
        return ProductModel.findOne({ _id });
    },
    //更新编辑
    updateList: async ({ _id, title, introduction, detail, cover, editTime }) => {
        console.log(_id);
        if (cover) {
            return ProductModel.updateOne(
                { _id },
                {
                    title,
                    introduction,
                    detail,
                    cover,
                    editTime,
                }
            );
        } else {
            ProductModel.updateOne(
                { _id },
                {
                    title,
                    introduction,
                    detail,
                    editTime,
                }
            );
        }
    },
};

module.exports = ProductService;
